package com.Service;

import com.Untils.SqlSessionFactoryUtils;
import com.mapper.BrandMapper;
import com.pojo.Brand;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandService {
    SqlSessionFactory factory=SqlSessionFactoryUtils.sqlSessionFactoryget();


    /*
    查询所有
     */

    public List<Brand> selectAll()
    {
        SqlSession sqlSession = factory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();

        sqlSession.close();

        return brands;
    }


    /**
     * 添加Brand对象
     * @param brand
     */
    public void add(Brand brand)
    {
        SqlSession sqlSession = factory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);

        sqlSession.commit();

        sqlSession.close();
    }

    /**
     * 修改Brand对象数据
     * @return
     */
    public Brand selectById(int id)
    {
        SqlSession sqlSession = factory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = mapper.selectById(id);

        sqlSession.commit();

        sqlSession.close();

        return brand;
    }

    public void update(Brand brand)
    {
        SqlSession sqlSession = factory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.update(brand);

        sqlSession.commit();

        sqlSession.close();
    }

    public void delete(int id)
    {
        SqlSession sqlSession = factory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteById(id);

        sqlSession.commit();

        sqlSession.close();
    }

}
